// import { onMounted } from 'vue'
import { apiGetLocationInfo, getCommonConfig } from '@/api/accompanies/common'
// import { useAppStoreWithOut } from '@/stores/modules/app'
import { useSystemStore } from '@/stores'
export default function commonHook() {
  return {
    getCommonConfig,

    checkUserLocation() {
      return new Promise((r, j) => {
        const system = useSystemStore();
        // const appStore = useAppStoreWithOut()
        const appStore = null;
        // const locationInfo = appStore.getUserLocation
        const locationInfo =system.location
        if (locationInfo) {
          r(locationInfo)
          return locationInfo
        } else {
          uni.getSetting({
            success(res) {
              if (!res.authSetting['scope.userLocation']) {
                // 未授权
                uni.authorize({
                  scope: 'scope.userLocation',
                  success() { //1.1 允许授权
                    uni.getLocation({
                      async success(res) {
                        const params = {
                          latitude: res.latitude,
                          longitude: res.longitude,
                        }
                        const result = await apiGetLocationInfo(params) as any
                        if (result.code) {
                          appStore.setUserLocation(result.data || {})
                          r(result.data)
                        }
                      },
                    })
                  },
                  fail() {    //1.2 拒绝授权
                    appStore.setUserLocation(null)
                    console.log('你拒绝了授权，无法获得周边信息')
                    j()
                  },
                })
              } else {
                uni.getLocation({
                  success: async function (res) {
                    const params = {
                      latitude: res.latitude,
                      longitude: res.longitude,
                    }
                    const result = await apiGetLocationInfo(params) as any
                    if (result.code) {
                      appStore.setUserLocation(result.data || {})
                      r(result.data)
                    }
                  },
                  fail(res) {
                    console.log('未授权:', res)
                    appStore.setUserLocation(null)
                    j()
                  },
                })
              }
            },
          })
        }
      })
    },

    openAuth() {
      return new Promise(function (t, r) {
        uni.openSetting({
          success: function () {
            t(!0)
          },
          fail: function (e) {
            t(!1)
          },
        })
      })
    },

    CheckIsAuth(t?: any) {
      return new Promise(function (r, n) {
        t = t || 'scope.userLocation', uni.getSetting({
          success: function (e) {
            r(e.authSetting[t])
          },
          fail: function () {
            r(!1)
          },
        })
      })
    },
  }
}
